using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Jabr.Logic.Exceptions.Handling;
using System.IO;
using System.Net.Mail;
using System.Net;

namespace Jabr.GUI.Forms
{
    public partial class ExceptionForm : AbstractExceptionForm
    {
        private bool m_ReadyToClose;

        public ExceptionForm()
        {
            InitializeComponent();

            m_ReadyToClose = false;
        }

        public override ExceptionInfo Info
        {
            get
            {
                return null;
            }
            set
            {
            }
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            FeedbackText.Enabled = OkBtn.Enabled = CancelBtn.Enabled = false;
            StatusLabel.Text = "Clearing temporary data...";
            FileThread.RunWorkerAsync();
        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            FeedbackText.Enabled = OkBtn.Enabled = CancelBtn.Enabled = false;
            StatusLabel.Text = "Sending...";
            EmailThread.RunWorkerAsync();
        }

        private void EmailThread_DoWork(object sender, DoWorkEventArgs e)
        {
            lock (this.ExceptionFilename)
            {
                MailMessage email = new MailMessage();
                email.From = new MailAddress(Jabr.Properties.Resources.ERROR_FROM_MAIL, Jabr.Properties.Resources.ERROR_FROM_NAME);
                email.To.Add(new MailAddress(Jabr.Properties.Resources.ERROR_TO_MAIL, Jabr.Properties.Resources.ERROR_TO_NAME));
                email.Subject = "[Jabr] Bug report";
                email.Body = this.FeedbackText.Text;
                email.Attachments.Add(new Attachment(this.ExceptionFilename));
                SmtpClient cli = new SmtpClient("smtp.gmail.com", 587);
                cli.UseDefaultCredentials = false;
                cli.EnableSsl = true;
                cli.Credentials = new NetworkCredential(Jabr.Properties.Resources.ERROR_FROM_MAIL, "mda91flx88");
                cli.Send(email);
                foreach (Attachment a in email.Attachments)
                    a.Dispose();
                email.Dispose();
            }
        }

        private void EmailThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StatusLabel.Text = "Clearing temporary data...";
            FileThread.RunWorkerAsync();
        }

        private void FileThread_DoWork(object sender, DoWorkEventArgs e)
        {
            lock (this.ExceptionFilename)
            {
                FileInfo fi = new FileInfo(this.ExceptionFilename);
                fi.Delete();
            }
        }

        private void FileThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StatusLabel.Text = "Thank you";
            m_ReadyToClose = true;
            this.Close();
        }

        private void ExceptionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!m_ReadyToClose)
            {
                e.Cancel = true;
                if (!FileThread.IsBusy && !EmailThread.IsBusy)
                    FileThread.RunWorkerAsync();
            }
        }
    }
}